简体   繁体   English

如何按字符串而不是按字符拆分列表中的值?

[英]How to split values in a list by string and not by characters?

I have read in a text file and split the rows up into two separate lists. 我已经阅读了一个文本文件,并将行分成两个单独的列表。 I'm trying to convert each of these two lists into a csv but I am running into a bit of a problem. 我正在尝试将这两个列表中的每一个都转换为csv,但是我遇到了一个问题。

My current python code (using python 3) to do the writing to csv looks like this: 我当前使用python 3编写的python代码(如下所示):

with open(bad_csv_file, 'w') as myfile:
      wr = csv.writer(myfile)
      for item in bad_csv:
        wr.writerow(item)
      myfile.close()

The rows I'm trying to write to csv look like this: 我要写入csv的行如下所示:

0000002 1 1E-10 1851 7 5 12 1851               
0000002 0 -97.6 22.2 -9999 -9999 0 0 97687 101325 45728 41.2 1e+05 1 0 0 0 0 0 -9999 -9999 -9999 -9999
0000003 1 1E-10 1851 7 10 12 1851               
0000003 0 -60 12 -9999 -9999 0 0 99993 101325 55402 25.7 1e+05 1 0 0 0 0 0 -9999 -9999 -9999 -9999
0000004 50 1E-10 1851 8 16 0 1851   

My current output looks like: 我当前的输出如下所示:

2,0,1,0,1,0,0,2, ,1,5,8, ,1, ,1,5,8, ,1,4,1,6, ,0, ,0, ,0, ,0, ,0, ,0, ,0, ,0, ,0, ,0, ,0, ,0, ,0, ,0, ,0, , , ,"
"
0,0,0,0,0,0,1, ,1,4, ,1,E,-,1,0, ,1,8,5,1, ,6, ,2,5, ,0, ,1,8,5,1, , , , , , , , , , , , , , , ,"
"
0,0,0,0,0,0,2, ,1, ,1,E,-,1,0, ,1,8,5,1, ,7, ,5, ,1,2, ,1,8,5,1, , , , , , , , , , , , , , , ,"
"
0,0,0,0,0,0,2, ,0, ,-,9,7,.,6, ,2,2,.,2, ,-,9,9,9,9, ,-,9,9,9,9, ,0, ,0, ,9,7,6,8,7, ,1,0,1,3,2,5, ,4,5,7,2,8, ,4,1,.,2, ,1,e,+,0,5, ,1, ,0, ,0, ,0, ,0, ,0, ,-,9,9,9,9, ,-,9,9,9,9, ,-,9,9,9,9, ,-,9,9,9,9,"
"
0,0,0,0,0,0,3, ,1, ,1,E,-,1,0, ,1,8,5,1, ,7, ,1,0, ,1,2, ,1,8,5,1, , , , , , , , , , , , , , , ,"

It seems to be splitting by character as opposed to the entire string of the number. 它似乎是按字符分割的,而不是数字的整个字符串。 Any help would be greatly appreciated, thanks! 任何帮助将不胜感激,谢谢!

Firstly, in your code you iterate over bad_csv which is unbound as far as the reader knows. 首先,在您的代码中,您遍历bad_csv ,据读者所知, bad_csv是未绑定的。 The naming could be somewhat more clear to make us understand what you mean to do. 为了使我们理解您的意思,命名可能会更清楚。

Secondly, if you iterate over a string it'll do it char by char . 其次,如果我们通过遍历string它会做到这一点charchar A row in the csvwriter is a sequence of strings or numbers . rowcsvwriter是序列字符串或数字 It'll iterate over that. 它将遍历。 What you need to do is to split your string on spaces . 您需要做的是将string拆分为spaces row = line.split(' ') Then row will be a sequence of your values. row = line.split(' ')然后row将是您的值序列。 If you want to remove the spaces in the end use row = line.rstrip().split(' ') 如果要删除最后的空格,请使用row = line.rstrip().split(' ')

Oh and you don't need to close a file if you opened it with with . 哦,如果你打开了它,你不必关闭文件with

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将字符串拆分为字符列表? - How do I split a string into a list of characters? list(string)如何将字符串拆分为python中的字符数组? - how does list(string) split the string to an array of characters in python? 如何将字符串拆分为字符列表,然后按每个字符连接 - How to split a string into list of characters and then concatenate by each character 如何将字符串拆分为字符并用浮点值替换字符以找到 Python 中原始字符串的总和? - How do I split string into characters and replace characters with float values to find the sum of original string in Python? 如何在python中将字符串拆分为字符? - How to split a string into characters in python? 如何将字符串拆分为字符矩阵 - How to split a string to a matrix of characters 将字符串插入列表而不拆分为字符 - Inserting a string into a list without getting split into characters 拆分字符串 > 单词和字符的子列表列表 - Split string > list of sublists of words and characters 如何按空格分割字符串的字符,然后按特殊字符和数字分割列表的结果元素,然后再次加入它们? - How to split the characters of a string by spaces and then resultant elements of list by special characters and numbers and then again join them? 如何按字符串拆分列表 - How to split a list by string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM