我有 csv 檔案,列中的資訊(id 和文本)如下例所示:
1, ?ildomos grindys
2, ?ildomos grindys, Rekuperacin? sistema
3,
4, Skalbimo ma?ina, Su baldais, ?aldytuvas, ?ildomos grindys
我想要的輸出是將 ID 傳輸到一行并將其與其文本相關聯(它用于資料庫)。由于 csv 檔案真的很大,我只給你一小部分來理解我想要什么:
| ID | Features
---------------- -------------
| 1 | ?ildomos grindys
| 2 | ?ildomos grindys
| 2 | Rekuperacin? sistema
| 3 | null
| 4 | Skalbimo ma?ina
| 4 | Su baldais
| 4 | ?aldytuvas
| 4 | ?ildomos grindys
我怎么能通過 python 做到這一點?謝謝 !
uj5u.com熱心網友回復:
這是一種執行您所要求的方法:
with open('infoo.txt', 'r', encoding="utf-8") as f:
records = []
rows = [[x.strip() for x in row.split(',')] for row in f.readlines()]
for row in rows:
for i in range(1, len(row)):
records.append([row[0], row[i] if row[i] else 'null'])
with open('outfoo.txt', 'w', encoding="utf-8") as g:
g.write('ID,Features\n')
for record in records:
g.write(f'{",".join(field for field in record)}\n')
# check the output file:
with open('outfoo.txt', 'r', encoding="utf-8") as f:
print('contents of output file:')
[print(row.strip('\n')) for row in f.readlines()]
輸出:
contents of output file:
ID,Features
1,?ildomos grindys
2,?ildomos grindys
2,Rekuperacin? sistema
3,null
4,Skalbimo ma?ina
4,Su baldais
4,?aldytuvas
4,?ildomos grindys
更新:
另一種方法是使用 pandas ( docs )。Pandas 提供了許多強大的方法來處理表格資料,但它也有一些學習曲線:
import pandas as pd
with open('infoo.txt', 'r', encoding="utf-8") as f:
records = []
rows = [[x.strip() for x in row.split(',')] for row in f.readlines()]
df = pd.DataFrame([[row[0], row[1:]] for row in rows], columns=['ID', 'Feature'])
print('Dataframe read from input file:'); print(df)
df = df.explode('Feature').reset_index(drop=True)
print('Dataframe with one Feature per row:'); print(df)
df.to_csv('outfoo.txt', index = False)
# check the output file:
df2 = pd.read_csv('outfoo.txt')
print('Dataframe re-read from output file:'); print(df2)
輸出
Dataframe read from input file:
ID Feature
0 1 [?ildomos grindys]
1 2 [?ildomos grindys, Rekuperacin? sistema]
2 3 []
3 4 [Skalbimo ma?ina, Su baldais, ?aldytuvas, ?ild...
Dataframe with one Feature per row:
ID Feature
0 1 ?ildomos grindys
1 2 ?ildomos grindys
2 2 Rekuperacin? sistema
3 3
4 4 Skalbimo ma?ina
5 4 Su baldais
6 4 ?aldytuvas
7 4 ?ildomos grindys
Dataframe re-read from output file:
ID Feature
0 1 ?ildomos grindys
1 2 ?ildomos grindys
2 2 Rekuperacin? sistema
3 3 NaN
4 4 Skalbimo ma?ina
5 4 Su baldais
6 4 ?aldytuvas
7 4 ?ildomos grindys
熊貓檔案的鏈接在這里:
- 資料框
- 爆炸
- to_csv
- read_csv
uj5u.com熱心網友回復:
您可以閱讀 CSV,然后將每一行附加到一個新串列中。
data.csv:
1, ?ildomos grindys
2, ?ildomos grindys, Rekuperacin? sistema
3,
4, Skalbimo ma?ina, Su baldais, ?aldytuvas, ?ildomos grindys
代碼:
import csv
data = []
with open('data.csv') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
for i in range(1, len(row)):
value = row[i].strip()
if value == "":
value = "null"
data.append([int(row[0]), value])
print(data)
輸出:
[[1, '?ildomos grindys'], [2, '?ildomos grindys'], [2, 'Rekuperacin? sistema'], [3, 'null'], [4, 'Skalbimo ma?ina'], [4, 'Su baldais'], [4, '?aldytuvas'], [4, '?ildomos grindys']]
參考:
- CSV 模塊上的 Python 檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/479367.html
上一篇:網頁抓取后將文本寫入csv
下一篇:拆分csv逗號分隔值
