簡體   English   中英

Python - 將空格分隔的.txt 文件轉換為 csv

[英]Python - converting a space delimited .txt file to csv

我有一些.txt 文件,其中有幾個數字,由空格分隔,形成一個網格。 在網格之前還有 6 行:

ncols         3120
nrows         2207
xllcorner     32
yllcorner     16
cellsize      30
NODATA_value  -9999
13 56 789 2 98 45 124 90 12 48 32 177 2 98 45 124 90 12 48 322 98 4 16 11 741
2 98 45 124 90 12 48 322 98 4 16 11 8 322 98 4 16 11 13 56 789 2 65 39 1 19 130
48 322 98 4 16 11 13 56 789 2 65 39 1 48 322 98 4 16 11 8 322 98 4 16 11 13 56
13 56 789 2 98 45 124 90 12 48 32 177 2 98 45 124 90 12 48 322 98 4 16 11 741
2 98 45 124 90 12 48 322 98 4 16 11 8 322 98 4 16 11 13 56 789 2 65 39 1 19 130
48 322 98 4 16 11 13 56 789 2 65 39 1 48 322 98 4 16 11 8 322 98 4 16 11 13 56

ETC...

這只是一個示例,因為它是一個包含數百個數字的巨大文件。

我想將每個數字乘以 integer(比如 50)。 我嘗試將 .txt 文件轉換為 .csv 文件,然后使用以下代碼進行乘法運算:

import pandas as pd

with open('file1.txt') as infile, open('file2.txt', 'w') as outfile:
    outfile.write(infile.read().replace(" ", ","))
df = pd.read_csv("file2.txt", delimiter=',')
df.to_csv('output.csv')

但我收到以下錯誤:

Traceback (most recent call last):
  File "C:/Users/Administrator/Desktop/file.py", line 7, in <module>
    df = pd.read_csv("file2.txt", delimiter=',')
  File "C:\Python36\lib\site-packages\pandas\io\parsers.py", line 676, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "C:\Python36\lib\site-packages\pandas\io\parsers.py", line 454, in _read
    data = parser.read(nrows)
  File "C:\Python36\lib\site-packages\pandas\io\parsers.py", line 1133, in read
    ret = self._engine.read(nrows)
  File "C:\Python36\lib\site-packages\pandas\io\parsers.py", line 2037, in read
    data = self._reader.read(nrows)
  File "pandas\_libs\parsers.pyx", line 859, in pandas._libs.parsers.TextReader.read
  File "pandas\_libs\parsers.pyx", line 874, in pandas._libs.parsers.TextReader._read_low_memory
  File "pandas\_libs\parsers.pyx", line 928, in pandas._libs.parsers.TextReader._read_rows
  File "pandas\_libs\parsers.pyx", line 915, in pandas._libs.parsers.TextReader._tokenize_rows
  File "pandas\_libs\parsers.pyx", line 2070, in pandas._libs.parsers.raise_parser_error
pandas.errors.ParserError: Error tokenizing data. C error: Expected 10 fields in line 7, saw 3121

當我嘗試將 this.txt 轉換為 csv 時,有人可以幫我理解有什么問題嗎? 非常感謝!

這將回答您的問題

# numbers.txt

ncols         3120
nrows         2207
xllcorner     32
yllcorner     16
cellsize      30
NODATA_value  -9999
13 56 789 2 98 45 124 90 12 48 32 177 2 98 45 124 90 12 48 322 98 4 16 11 741
2 98 45 124 90 12 48 322 98 4 16 11 8 322 98 4 16 11 13 56 789 2 65 39 1 19 130
48 322 98 4 16 11 13 56 789 2 65 39 1 48 322 98 4 16 11 8 322 98 4 16 11 13 56
13 56 789 2 98 45 124 90 12 48 32 177 2 98 45 124 90 12 48 322 98 4 16 11 741
2 98 45 124 90 12 48 322 98 4 16 11 8 322 98 4 16 11 13 56 789 2 65 39 1 19 130
48 322 98 4 16 11 13 56 789 2 65 39 1 48 322 98 4 16 11 8 322 98 4 16 11 13 5656
13 56 789 2 98 45 124 90 12 48 32 177 2 98 45 124 90 12 48 322 98 4 16 11 741
2 98 45 124 90 12 48 322 98 4 16 11 8 322 98 4 16 11 13 56 789 2 65 39 1 19 130
48 322 98 4 16 11 13 56 789 2 65 39 1 48 322 98 4 16 11 8 322 98 4 16 11 13 56
f = open('test.txt')
result = f.readlines()[6:]

final = []

for ele in result:
    i = ele.replace(' ','\t')
    final.append(i)

for ele in final:
    print(ele))

        

請使用以下代碼跳過從文本文件中讀取前 6 行:

for _ in range(6):
    next(infile)

您可以找到此實現的完整代碼,如下所示:

import pandas as pd
with open('file1.txt') as infile, open('file2.txt', 'w') as outfile:
    for _ in range(6):
        next(infile)
    outfile.write(infile.read())
df = pd.read_csv("file2.txt", delimiter=' ')
print(df.head())
#df.to_csv('output.csv')

這可以通過使用 pandas read_csv方法的skiprowssepindex_colheader關鍵字參數來實現。

import pandas as pd

# reading txt file, skipping 6 rows, separator is ' '
df = pd.read_csv('a.txt',skiprows=6, sep='\s', index_col=False, header=None)

for i in range(len(df)):
    df[i] = df[i]*50

使用單個空格作為分隔符sep='\s' 並確保第一行和 col 不用作 header 和索引值,使用index_col=Falseheader=None

如果您只想使用 pandas:

mydata = pd.read_csv('myfile.txt', skiprows=6,  delim_whitespace=True, header=None, names=range(27) )

請看下面數據框的截圖:

屏幕輸出不足

請注意,我將 names 參數設置為可容納數據中最長列的范圍。 數據集中會有 Nan,但您也可以輕松處理。

然后您可以根據需要跟進其他操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM