联邦学习好难啊 2021-06-02 19:27 采纳率: 66.7%
浏览 35

如何对于一个大数据量的文本进行数据的修改

我这边需要对一个原本是.csv格式保存的文件进行文本上的一个修改,原先考虑的是通过Ubuntu16.04自带的LibreOffice进行操作,但是通过LibreOffice打开之后报了一个错误如下图所示:

我在一开始的情况下并没有对此有任何的操作,于是继续打开了,然后在修改完数据的内容并保存为新的文件之后,我打开新的文件发现平白少了60万行的数据,原数据的大小大概为160万行的数据,通过LibreOffiec打开并完成修改之后就变成了100万左右,而这些数据在我之后的使用过程中产生了极大的影响,于是我希望能找到一个方法,如何解决这个无法打开过大数据量的数据文件的问题,或者通过别的方法我能完成数据的修改。

另外,对于数据的格式,以及数据所期望的修改形式如下:

我希望将如下的原数据的A382147一直到A582147的数据0替换成图二中的10或者其他的1到10之间的数,然后再把F这一列保存有文本数据的F382147到F582147的文本内容末尾都添加上“。。。well”这七个字符,所以有什么办法能在不损失数据量的情况修改原数据呢

 

 

 

  • 写回答

1条回答 默认 最新

  • 小P聊技术 2021-06-02 21:21
    关注

    虽然将这些CSV文件中的数据加载到数据库中非常简单,但是有时您可能无权访问数据库服务器和/或不想经历设置数据库的麻烦。服务器。 如果要长期处理数据集,则绝对应将数据加载到某种类型的数据库(mySQL,postgreSQL等)中,但是如果您只需要对数据集进行一些快速检查/测试/分析,数据,下面是使用python,pandas和sqllite查看这些大文件中数据的一种方法。

    To get started, you’ll need to import pandas and sqlalchemy. The commands below will do that.

    首先,您需要导入pandas和sqlalchemy。 下面的命令将执行此操作。

    
     
    1. import pandas as pd

    2. from sqlalchemy import create_engine

    Next, set up a variable that points to your csv file.  This isn’t necessary but it does help in re-usability.

    接下来,设置一个指向您的csv文件的变量。 这不是必需的,但确实有助于重用。

    file = '/path/to/csv/file'
    
    

    With these three lines of code, we are ready to start analyzing our data. Let’s take a look at the ‘head’ of the csv file to see what the contents might look like.

    有了这三行代码,我们就可以开始分析数据了。 让我们看一下csv文件的“头”,看看内容可能是什么样子。

    print pd.read_csv(file, nrows=5)
    
    

    This command uses pandas’ “read_csv” command to read in only 5 rows (nrows=5) and then print those rows to the screen. This lets you understand the structure of the csv file and make sure the data is formatted in a way that makes sense for your work.

    此命令使用pandas的“ read_csv”命令仅读取5行(行数= 5),然后将这些行打印到屏幕上。 这使您了解csv文件的结构,并确保以对您的工作有意义的方式格式化数据。

    Before we can actually work with the data, we need to do something with it so we can begin to filter it to work with subsets of the data. This is usually what I would use pandas’ dataframe for but with large data files, we need to store the data somewhere else. In this case, we’ll set up a local sqllite database, read the csv file in chunks and then write those chunks to sqllite.

    在实际使用数据之前,我们需要对其进行处理,以便可以开始对其进行过滤以与数据的子集一起使用。 这通常是我将使用pandas数据框的方式,但是对于大型数据文件,我们需要将数据存储在其他位置。 在这种情况下,我们将建立一个本地sqllite数据库,分块读取csv文件,然后将那些块写入sqllite。

    To do this, we’ll first need to create the sqllite database using the following command.

    为此,我们首先需要使用以下命令创建sqllite数据库。

    csv_database = create_engine('sqlite:///csv_database.db')
    
    

    Next, we need to iterate through the CSV file in chunks and store the data into sqllite.

    接下来,我们需要分批遍历CSV文件并将数据存储到sqllite中。

    
     
    1. chunksize = 100000

    2. i = 0

    3. j = 1

    4. for df in pd.read_csv(file, chunksize=chunksize, iterator=True):

    5. df = df.rename(columns={c: c.replace(' ', '') for c in df.columns})

    6. df.index += j

    7. i+=1

    8. df.to_sql('table', csv_database, if_exists='append')

    9. j = df.index[-1] + 1

    With this code, we are setting the chunksize at 100,000 to keep the size of the chunks managable, initializing a couple of iterators (i=0, j=0) and then running a through a for loop.  The for loop read a chunk of data from the CSV file, removes space from any of column names, then stores the chunk into the sqllite database (df.to_sql(…)).

    使用此代码,我们将块大小设置为100,000,以保持可管理的块大小,并初始化几个迭代器(i = 0,j = 0),然后运行一个for循环。 for循环从CSV文件中读取数据块,从任何列名称中删除空间,然后将数据块存储到sqllite数据库(df.to_sql(…))中。

    This might take a while if your CSV file is sufficiently large, but the time spent waiting is worth it because you can now use pandas ‘sql’ tools to pull data from the database without worrying about memory constraints.

    如果您的CSV文件足够大,则可能要花一些时间,但是花在等待上的时间是值得的,因为您现在可以使用pandas“ sql”工具从数据库中提取数据,而不必担心内存限制。

    To access the data now, you can run commands like the following:

    要立即访问数据,您可以运行以下命令:

    df = pd.read_sql_query('SELECT * FROM table', csv_database)
    

    Of course, using ‘select *…’ will load all data into memory, which is the problem we are trying to get away from so you should throw from filters into your select statements to filter the data. For example:

    当然,使用“ select *…”会将所有数据加载到内存中,这是我们试图摆脱的问题,因此您应该将过滤器放入select语句中以过滤数据。 例如:

    df = pd.read_sql_query('SELECT COl1, COL2 FROM table where COL1 = SOMEVALUE', csv_database)
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料