Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 将中的文本转换为单独列下的csv文件_Python 3.x_Csv_Text_Export To Csv - Fatal编程技术网

Python 3.x 将中的文本转换为单独列下的csv文件

Python 3.x 将中的文本转换为单独列下的csv文件,python-3.x,csv,text,export-to-csv,Python 3.x,Csv,Text,Export To Csv,我在一个.txt文件中有以下内容 1.['LG','Samsung','Asus','HP','Apple','HTC'] 2.['covid','vaccine','infection','cure','chloroquine'] 3.['p2p','crypto','bitcoin','litecoin','blockchain'] 如何将上述内容转换为不同列下的csv文件 我现在的代码是 import csv with open('Full_txt_results.txt', 'r')

我在一个.txt文件中有以下内容

1.['LG','Samsung','Asus','HP','Apple','HTC']
2.['covid','vaccine','infection','cure','chloroquine']
3.['p2p','crypto','bitcoin','litecoin','blockchain']
如何将上述内容转换为不同列下的csv文件

我现在的代码是

import csv

with open('Full_txt_results.txt', 'r') as in_file:
    stripped = (line.strip() for line in in_file)
    lines = (line.split(",") for line in stripped if line)
    with open('textlabels.csv', 'w') as out_file:
        writer = csv.writer(out_file)
        writer.writerows(lines)

代码当前以csv的以下格式给出结果

Column 1  Column2   column 3   column 4     Column 5        column 6
['LG'    'Samsung'  'Asus'       'HP'       'Apple'        'HTC']
['covid' 'vaccine' 'infection'  'cure'    'chloroquine']
['p2p'   'crypto'  'bitcoin'   'litecoin'  'blockchain']

文本被分门别类地放在不同的栏目里

所需的理想输出格式如下

Column 1  Column2     column 3   
LG        Covid         p2p
Samsung   Vaccine      crypto
Asus      Infection    bitcoin
HP        cure         litecoin
Apple     chloroquine  blockchain  
HTC

使用
ast
模块将字符串转换为列表对象,然后使用
writerow
方法写入csv

Ex:

import csv
import ast

with open('Full_txt_results.txt') as in_file, open('textlabels.csv', 'w', newline="") as out_file:
    writer = csv.writer(out_file)
    data = [ast.literal_eval(line.strip().split(".")[1]) for line in in_file]      #If you do not have column number(1.,2.,...) Use [ast.literal_eval(line.strip()) for line in in_file]
    for row in zip(*data):
        writer.writerow(row)
LG,covid,p2p
Samsung,vaccine,crypto
Asus,infection,bitcoin
HP,cure,litecoin
Apple,chloroquine,blockchain

演示

import csv
import ast

with open(filename) as in_file, open(outfile, 'w', newline="") as out_file:
    writer = csv.writer(out_file)
    data = [ast.literal_eval(line.strip()) for line in in_file]
    for row in zip(*data):
        writer.writerow(row)
SRC txt文件

['LG','Samsung','Asus','HP','Apple','HTC']
['covid','vaccine','infection','cure','chloroquine']
['p2p','crypto','bitcoin','litecoin','blockchain']
输出:

import csv
import ast

with open('Full_txt_results.txt') as in_file, open('textlabels.csv', 'w', newline="") as out_file:
    writer = csv.writer(out_file)
    data = [ast.literal_eval(line.strip().split(".")[1]) for line in in_file]      #If you do not have column number(1.,2.,...) Use [ast.literal_eval(line.strip()) for line in in_file]
    for row in zip(*data):
        writer.writerow(row)
LG,covid,p2p
Samsung,vaccine,crypto
Asus,infection,bitcoin
HP,cure,litecoin
Apple,chloroquine,blockchain

你的错误是什么?没有错误。只是没有得到正确的格式。请参阅上面的当前输出和所需的理想输出。因此不是:编写代码服务。如果您需要关于特定错误的帮助,那么在解析第一行数据之后,请求帮助是合适的。我还收到了错误
ValueError:格式错误的节点或字符串:[“['LG'、'Samsung'、'Asus'、'HP'、'Apple'、'HTC']”]